//    This file is part of Open WordSearch.
//
//    Open WordSearch is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Open WordSearch is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Open WordSearch.  If not, see <http://www.gnu.org/licenses/>.
//
//        Copyright 2009, 2010 Brendan Dahl <dahl.brendan@brendandahl.com>
//              http://www.brendandahl.com
package com.abhipedro.wordsearch.view.runnables;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.LinkedList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.abhipedro.wordsearch.model.HighScore;
import com.abhipedro.wordsearch.model.Preferences;
import com.abhipedro.wordsearch.util.Constants;
import com.abhipedro.wordsearch.util.ConversionUtil;
import com.abhipedro.wordsearch.view.R;
import com.abhipedro.wordsearch.view.WordSearchActivity;
import com.abhipedro.wordsearch.view.controller.WordSearchActivityController;

/**
 * this class stores the action of requesting the user's initials to store a new
 * high score
 */
public class HighScoresInitials implements Runnable,
		DialogInterface.OnClickListener {

	final private HighScore hs;
	final private WordSearchActivity wordSearch;
	final private WordSearchActivityController controller;
	final private Preferences prefs;

	int gameMode = 0;

	public HighScoresInitials(WordSearchActivityController controller,
			HighScore hs, WordSearchActivity wordSearch, Preferences prefs) {
		this.controller = controller;
		this.hs = hs;
		this.wordSearch = wordSearch;
		this.prefs = prefs;
	}

	public void run() {
		AlertDialog.Builder builder = new AlertDialog.Builder(wordSearch);
		String replaceable = "\nFinal Score : " + hs.getScore();
		builder.setMessage(wordSearch.getString(R.string.enter_initials)
				.replace("%score", replaceable	+ "\nTime : " + ConversionUtil.formatTime.format(new Date(hs
										.getTime())) + " "));

		builder.setPositiveButton(R.string.new_game, this);
		builder.setNegativeButton(R.string.submitScore, this);
		builder.show();

	}

	public void onClick(DialogInterface dialog, int which) {

		SharedPreferences _settings = wordSearch.getSharedPreferences(
				Constants.PREFS_NAME, 0);
		String name = _settings.getString(Constants.PREFS_PLAYER_NAME, "");

		LinkedList<HighScore> scores = prefs.getTopScores();
		hs.setInitials("");
		scores.add(hs);
		prefs.setTopScores(scores);

		if (which == DialogInterface.BUTTON_POSITIVE) {
			controller.newWordSearch();
		} else {
			if (which == DialogInterface.BUTTON_NEGATIVE) {
				if (!TextUtils.isEmpty(name)) {
					hs.setInitials(name);
				} else {
					hs.setInitials("Anonymous");
					name = "Anonymous";
				}
				scores.add(hs);
				prefs.setTopScores(scores);
				HighScoresShow hsShow = new HighScoresShow(controller,
						wordSearch, true);
				hsShow.run();

				String frase = name + hs.getScore() + Constants.ONLINE_URL_KEY; 
				MessageDigest m;
				String md5 = null;
				try {
					m = MessageDigest.getInstance("MD5");
					m.update(frase.getBytes(), 0, frase.length());
					md5 = new BigInteger(1, m.digest()).toString(16);
					while (md5.length() < 32) {
						md5 = "0" + md5;
					}
				} catch (NoSuchAlgorithmException e) {
				}

				prefs.setOnlineURL(Constants.URL_Scores + "?name=" + name
						+ "&score=" + hs.getScore() + "&cod=" + md5);
				final Intent i2;
				i2 = new Intent(wordSearch, OnlineHighScore.class);
				wordSearch.startActivity(i2);

			} else
				wordSearch.runOnUiThread(new NewGameDialog(controller,	wordSearch, null));
		}
	}

}
